package com.mcj.quartz.controller;

import com.mcj.quartz.entity.MyQuartzJobPO;
import com.mcj.quartz.repository.MyQuartzJobRepository;
import com.mcj.quartz.service.QuartzService;
import com.mcj.quartz.vo.requestvo.MyQuartzJobRequestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : maichaojiu
 * @Descirption :
 * @date : 2018/4/8
 */
@RestController
@RequestMapping(value = "/job")
@Api(value = "定时任务管理的API", description = "定时任务的CRUD操作")
public class MyQuartzJobController {

    @Autowired
    private QuartzService quartzService;

    @Autowired
    private MyQuartzJobRepository myQuartzJobRepository;

    @ApiOperation(value = "新增定时任务", notes = "新增定时任务 ", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vo", value = "定时任务的vo", required = true, dataType = "MyQuartzJobRequestVO")
    })
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Map<String, Object> add(@RequestBody @Valid MyQuartzJobRequestVO vo){
        Map<String, Object> map = new HashMap<String, Object>();
        //判断重复
        //保存
        MyQuartzJobPO po = new MyQuartzJobPO();
        BeanUtils.copyProperties(vo, po);
        myQuartzJobRepository.save(po);
        map.put("msg","success");
        return map;
    }

    @ApiOperation(value = "刷新定时任务", notes = "刷新定时任务 ", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @RequestMapping(value = "/refresh", method = RequestMethod.GET)
    public Map<String, Object> reflash(){
        Map<String, Object> map = new HashMap<String, Object>();
        quartzService.refreshJob();
        map.put("msg","success");
        return map;
    }

    @ApiOperation(value = "启动定时任务", notes = "启动定时任务 ", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vo", value = "定时任务的vo", required = true, dataType = "MyQuartzJobRequestVO")
    })
    @ResponseBody
    @RequestMapping(value = "/start", method = RequestMethod.PUT)
    public Map<String, Object> start(@RequestBody MyQuartzJobRequestVO vo){
        Map<String, Object> map = new HashMap<String, Object>();
        //判断状态
        //保存
        MyQuartzJobPO po = new MyQuartzJobPO();
        BeanUtils.copyProperties(vo, po);
        myQuartzJobRepository.save(po);

        quartzService.start(vo.getJobName(), vo.getJobGroup(), vo.getCron());
        map.put("msg","success");
        return map;
    }

    @ApiOperation(value = "暂停定时任务", notes = "暂停定时任务 ", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vo", value = "定时任务的vo", required = true, dataType = "MyQuartzJobRequestVO")
    })
    @ResponseBody
    @RequestMapping(value = "/stop", method = RequestMethod.PUT)
    public Map<String, Object> stop(@RequestBody MyQuartzJobRequestVO vo){
        Map<String, Object> map = new HashMap<String, Object>();
        //修改状态
        MyQuartzJobPO po = new MyQuartzJobPO();
        BeanUtils.copyProperties(vo, po);
        myQuartzJobRepository.save(po);
        quartzService.stop(vo.getJobName(), vo.getJobGroup());
        map.put("msg","success");
        return map;
    }

    @ApiOperation(value = "立即执行一次定时任务", notes = "立即执行一次定时任务 ", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vo", value = "定时任务的vo", required = true, dataType = "MyQuartzJobRequestVO")
    })
    @ResponseBody
    @RequestMapping(value = "/execute", method = RequestMethod.PUT)
    public Map<String, Object> execute(@RequestBody MyQuartzJobRequestVO vo){
        Map<String, Object> map = new HashMap<String, Object>();
        quartzService.execute(vo.getJobName(), vo.getJobGroup());
        map.put("msg","success");
        return map;
    }

    @ApiOperation(value = "删除定时任务", notes = "删除定时任务 ", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vo", value = "定时任务的vo", required = true, dataType = "MyQuartzJobRequestVO")
    })
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Map<String, Object> delete(@RequestBody MyQuartzJobRequestVO vo){
        Map<String, Object> map = new HashMap<String, Object>();
        //删除
        MyQuartzJobPO po = new MyQuartzJobPO();
        BeanUtils.copyProperties(vo, po);
        myQuartzJobRepository.delete(po);
        quartzService.delete(vo.getJobName(), vo.getJobGroup());
        map.put("msg","success");
        return map;
    }
}
